<include file="../header" />
<style>
    .keyword-input{
        margin-top: 1rem;
    }
    .keyword-btn{
        margin-top: 0.5rem;
        width: 120px;
        float: left;
    }
    .kw-btn{
        width: 50px;
    }
    .shut-down{
        width: 12px;
        height: 12px;
        position: relative;
        left: -0.5rem;
        top: -0.9rem;
        cursor: pointer;
    }
</style>
<script type="text/javascript" src="/Template/js/vue.min.js"></script>

<body class="J_scroll_fixed" id="container">
<script type="text/javascript">
    function onlyNumber(obj){
        //得到第一个字符是否为负号
        var t = obj.value.charAt(0);
        //先把非数字的都替换掉，除了数字和.
        if(obj.value != 0)
        obj.value = obj.value.replace(/[^\d\.]/g,'').replace(/^0{1,}/g,'');
        //必须保证第一个为数字而不是.
        obj.value = obj.value.replace(/^\./g,'');
        //保证只有出现一个.而没有多个.
        obj.value = obj.value.replace(/\.{2,}/g,'.');
        //保证.只出现一次，而不能出现两次以上
        obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
        //只能输入两位小数
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
        //如果第一位是负号，则允许添加
        if(t == '-'){
            return;
        }
    }

    $(function() {
        $("input[name=checkAll]").click(function() {
            var checkboxs = $("input.checkItem");
            if($("input[name=checkAll]").is(':checked')) {
                for(var i = 0 ;i<checkboxs.length;i++) {
                    checkboxs[i].checked = true;
                }
            }else{
                for(var i = 0 ;i<checkboxs.length;i++) {
                    checkboxs[i].checked = false;
                }
            }
        });

        var app = new Vue({
            el: '#container',
            data: {
                keywordList: []
            },
            methods: {
                remove: function(index) {
                    app.keywordList.splice(index,1);
                }
            }
        })

        $("a.add-keyword").bind("click", function() {

            var code = $(this).prev().find("input").val();
            var keyword = $(this).prev().prev().find("input").val();

            if(keyword == "") {
                alert("关键词不能为空");
                return false;
            }

            if(code == "") {
                alert("编码不能为空");
                return false;
            }

            var data = [];

            data.code = code;
            data.keyword = keyword;

            app.keywordList.push(data);

            $(this).prev().find("input").val("");
            $(this).prev().prev().find("input").val("");

        });

        $("button#upload").click(function(){
            //上传文件
            $.ajaxFileUpload({
                url:"{:U('activity/uploadImage')}",//处理图片脚本
                secureuri :false,
                fileElementId :'fileToUpload',//file控件id
                dataType : 'json',
                success : function (data, status){
                    $("input[name=imageUrl]").val(data);
                    $("img[name=preShow]").attr("src", data);
                },
                error: function(data, status, e){
                    alert(e);
                }
            })

        });

        $("button.btn_submit").click(function() {
            var discount = $("input[name=discount]").val();
            if(discount > 1 || discount < 0 || discount == ""){
                alert("折扣应为0~1内的小数");
                $("input[name=discount]").focus();
                return false;
            }

            var size = $("textarea[name=size]").val();
            var sizeList = size.split("/");
            var newSizeList = sizeList.slice().sort();
            for (var j = 0; j < sizeList.length; j++) {
                if(newSizeList[j] == newSizeList[j+1]){
                    alert("规格有重复数据：" + newSizeList[j]);
                    $("textarea[name=size]").focus();
                    return false;
                }
            }

            var checkboxs = $("input.checkItem");
            var id = "";
            var price = "";
            for (var i = 0; i < checkboxs.length; i++) {
                if(checkboxs[i].checked == true){
                    var priceName = "price_"+checkboxs[i].name;
                    if($("input[name="+priceName+"]").val() == ""){
                        alert("请填写单价");
                        $("input[name="+priceName+"]").focus();
                        return false;
                    }
                    if(id != ""){
                        id += ",";
                        price += ",";
                    }
                    id += checkboxs[i].name;
                    price += $("input[name="+priceName+"]").val();
                }
            }
            $("input[name=agentId]").val(id);
            $("input[name=agentPrice]").val(price);

//            if(id == ""){
//                alert("未选择单据");
//                $("input[name=checkAll]").focus();
//                return false;
//            }
//            alert(id+"|" + price);
//            return false;
            $("button.btn_submit").hide();
//            $("form").submit();

        })


    })

</script>
<style type="text/css">
    .btn-danger,.btn-info,.btn-success,.btn-warning{
        width: 50px;
        /*height: 18px;*/
        padding: 2px 0px 2px 0px;
        margin: 1px 0px 1px 0px;
    }
    .price{
        width: 70px;
    }
</style>
<script type="text/javascript"  src="__TMP__/assets/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript"  src="__TMP__/assets/js/ueditor/ueditor.all.min.js"></script>
<div class="wrap jj">
    <div class="common-form">
        <form method="post" class="form-horizontal J_ajaxForm" action="{:U('finishedProduct/addPost')}" onkeydown="if(event.keyCode==13)return false;">
            <fieldset>
                <div class="control-group">
                    <label class="control-label">分类：</label>
                    <div class="controls">
                        <select name="classId">
                            {:getCommonSelectOptionHtml($info['class_id'], "Class", "class_id", "class_name", null, $user['enterprise_code'], 1)}
                        </select>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">供应商：</label>
                    <div class="controls">
                        <!--select name="supplier">
                            <option value='国产'>国产</option>
                            <option value='进口'>进口</option>
                            <option value='奥托博克'>奥托博克</option>
                            <option value='途安'>途安</option>
                        </select-->
                        <select name="supplier_id">
                            {:getCommonSelectOptionHtml($info['supplier_id'], "Supplier", "supplier_id", "supplier_name", null, $user['enterprise_code'], 1)}
                        </select>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">名称：</label>
                    <div class="controls">
                        <input type="text" class="input" name="productName" value="">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">产品编码：</label>
                    <div class="controls">
                        <input type="text" class="input" name="number" value="">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">规格：</label>
                    <div class="controls">
                        <textarea style="height:9rem;" name="size"></textarea>
                        例如： 0/1/2/3/4/5
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">尺寸：</label>
                    <div class="controls">
                        <textarea style="height:9rem;" name="size_detail"></textarea>
                        例如： 97mm/102.1mm/112.1mm/122.2mm/133.7mm
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">尺寸说明：</label>
                    <div class="controls">
                        <input type="text" class="input" name="size_comment" value="">
                        例如： [足长]
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">单位：</label>
                    <div class="controls">
                        <input type="text" class="input" name="unit" value="">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">定价：</label>
                    <div class="controls">
                        <input type="text" class="input" name="price" value="">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">最低折扣：</label>
                    <div class="controls">
                        <input type="text" class="input" name="discount" value="">
                        0~1的小数
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">图片:</label>
                    <div class="controls">
                        <input id="fileToUpload" type="file" name="fileToUpload" style="width:170px;">
                        <button type="button" class="btn btn-primary" id="upload">上传</button>
                        <input type="hidden" name="imageUrl" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">图片预览:</label>
                    <div class="controls">
                        <img src="" name="preShow" style="width:200px;height:200px;" />
                    </div>
                </div>
                <!--div class="control-group">
                    <label class="control-label">编码：</label>
                    <div class="controls">
                        <input type="text" class="input" name="code" value="">
                    </div>
                </div-->
                <div class="control-group">
                    <label class="control-label">功能：</label>
                    <div class="controls">
                        <textarea style="height:6rem;" name="function"></textarea>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">适应症：</label>
                    <div class="controls">
                        <textarea style="height:6rem;" name="indication"></textarea>
                    </div>
                </div>
                <hr class="half-rule">
                <table class="table table-hover table-bordered" id="viewTabs" style="width: 500px">
                    <thead>
                    <tr>
                        <th style="min-width: 10px; width: 10px"><input type="checkbox" name="checkAll"></th>
                        <th style="min-width: 100px; width: 200px">代理商</th>
                        <th style="min-width: 100px; width: 100px">单价</th>
                    </tr>
                    </thead>
                    <tbody>
                    <foreach name="list" item="vo">
                        <tr>
                            <php>
                                $agent_checked=($vo['agent_price'] != "")?"checked":"";
                            </php>
                            <td><input type="checkbox" class="checkItem" name="{$vo.agent_id}" {$agent_checked}></td>
                            <td style="font-size: 1rem">{$vo.agent_id}.{$vo.agent_name}</td>
                            <if condition="$vo['agent_name'] eq '员工下单'">
                                <td style="font-size: 1rem"><input type="text" class="input price" name="price_{$vo.agent_id}" value="0" readonly>&nbsp;元</td>
                                <else/>
                                <td style="font-size: 1rem"><input type="text" class="input price" name="price_{$vo.agent_id}" value="{$vo.agent_price}" onkeyup="onlyNumber(this)">&nbsp;元</td>
                            </if>
                        </tr>
                    </foreach>
                    </tbody>
                </table>
                <input type="hidden" name="agentId" value="" style="width: 200px"/>
                <input type="hidden" name="agentPrice" value="" style="width: 500px"/>
            </fieldset>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary btn_submit">添加</button>
                <a class="btn" href="__URL__">返回</a>
            </div>
        </form>
    </div>
</div>
<script src="__ROOT__/statics/js/common.js"></script>
</body>
</html>